<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dosen_wali extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_dosen_wali');
		//$this->output->enable_profiler(true);
	}
	
	function index(){
		$this->load->view('master/dosen_wali');
	}
	
	
	function grid(){
		$data = $this->mdl_dosen_wali->getdata();
		echo $this->mdl_dosen_wali->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		
		$data['kode'] = '';
		$data['kd_dosen'] = '';
		//$data['nidn'] = '';
		$data['kd_fakultas'] = '';
		$data['kd_prodi'] = '';
		$data['dosen_wali'] = 'A';
		//$data['no_sk'] = '';
		$this->load->view('master/dosen_wali_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_dosen_wali->getdataedit($kode);
		
		$data['kode'] = $kode;
		$data['kd_dosen'] = $kode;
		//$data['nidn'] = $r->row()->nidn;		
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['dosen_wali'] = $r->row()->dosen_wali;
		//$data['no_sk'] = $r->row()->no_sk;
		$data['aksi'] = 'edit';
		$this->load->view('master/dosen_wali_form', $data);
	}
	
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		if($aksi == 'add'){
			$this->form_validation->set_rules("kd_dosen", 'Kode Dosen', 'trim|required|xss_clean');
			$this->form_validation->set_rules("dosen_wali", 'Status Dosen Wali', 'trim|required|xss_clean');
			//$this->form_validation->set_rules("no_sk", 'No SK', 'trim|required|xss_clean');
		}else{
			$this->form_validation->set_rules("kode", 'Kode Dosen', 'trim|required|xss_clean');
		}
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_dosen_wali->InsertOnDb($data);
			}else { // edit
				$result=$this->mdl_dosen_wali->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	} 
	
	function delete($kode){
		$result = $this->mdl_dosen_wali->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	} 
	
	function pdf(){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_dosen_wali->get_pdf();
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}
		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',16);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'Data Dosen Wali';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Dosen Wali.pdf", "I");
	}
}